package cn.chuengxu.controller;

import cn.chuengxu.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate; // 用来发起请求

    @HystrixCommand(fallbackMethod = "fallbackMethod")   //方法熔断
    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Long id){
        // 设置发送的路径
        String url = "http://user-server/getUserById/" + id;
        return restTemplate.getForObject(url,User.class);
    }

    /**======================================================================================
    * @Title: fallbackMethod
    * @Description: 上面方法失效后熔断方法返回的拖底数据
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/28 19:52
    * @Parameters: [id]
    * @Return cn.chuengxu.domain.User
    ======================================================================================*/
    public User fallbackMethod(@PathVariable Long id) {

        return User.builder().id(-1L).name("系统繁忙").address("请稍后重试").build();
    }


}
